<?php

/**
 * author : Tran Minh Toan
 */

//Require model files
require_once ROOT_APPLICATION_MODULES_DEFAULT_MODELS . DS . 'Producer.php';
//Require objects files
require_once ROOT_APPLICATION_MODULES_DEFAULT_OBJECTS . DS . 'Producer.php';
//Require forms files
require_once ROOT_APPLICATION_MODULES_DEFAULT_FORMS . DS . 'Producer.php';

class ProducerController extends Zend_Controller_Action{
    
    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private  $modelProducer;
    private $_redirector;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////
    
    
    public function preDispatch() {

    }
    
    public function init(){
        $layoutPath = APPLICATION_PATH . '/templates/default';
        $option = array('layout' => 'index',
        'layoutPath' => $layoutPath);
        
        Zend_Layout::startMvc($option);
        $this->modelProducer = new End_Models_Producer();
        $this->_redirector = $this->_helper->getHelper('Redirector');
    }
    
    ///////////////////////
    // U T I L I T I E S //
    ///////////////////////
   
        
    ///////////////////
    // A C T I O N S //
    ///////////////////
    //INDEX ACTION
    public function indexAction() { 
        $tmpArrProducer = $this->modelProducer->fetchAll();
        $this->view->arrProducer = $tmpArrProducer;
    }
    
    public function addAction(){
        $formProducer = new Application_Form_Producer();
        
        if($this->getRequest()->isGet()){
            $formProducer->Producer_id->setValue($Producer_id);
        }  else {
            $cancel = $this->getRequest()->getPost('cancel', '0');
            if($cancel){
                $this->_redirector->gotoUrl('Producer/index');
            }
            
            $formData = $this->getRequest()->getPost();
            
            if($formProducer->isValid($formData)){
                $Producer = new Producer();
    
                $Producer->producer_id            = $formData['producer_id'];
                $Producer->producer_name     = $formData['producer_name'];
                
                $this->modelProducer->_insert($Producer);
                $this->_redirector->gotoUrl('Producer/index');
            }else{
                $formProducer->populate($formData);
            }
        }
        $this->view->form = $formProducer;
    }


    public function editAction(){
        
        $id = $this->_getParam('id','0');
        $Producer = $this->modelProducer->_find($id);
        $formProducer = new Application_Form_Producer();
        
        if(!$this->getRequest()->isGet()){
            if(isset ($Producer)){
                $formProducer->Producer_id->setValue($id);
                $cancel=$this->getRequest()->getPost('cancel', '0');
                if($cancel){
                    $this->_redirector->gotoUrl('Producer/index');
                }
                
                $formData=$this->getRequest()->getPost();
                if($formProducer->isValid($formData)){
                    
                    $Producer->Producer_date_create  = date('Y-m-d');
                    $Producer->user_id_employee      = '1';// gán tạm.
                    $Producer->product_id            = $formData['product_id'];
                    $Producer->user_id_customer      = $formData['user_id_customer'];
                    $Producer->isReturned           = $formData['isReturned'];
                    
                    $id = $this->modelProducer->_update($Producer);
                    //$this->view->isReturned = $Producer->isReturned;
                    $this->_redirector->gotoUrl('Producer/index');
                }else{
                    $formProducer->populate($formData);
                }
            }else{
                $this->_redirector->gotoUrl('Producer/index');
            }
            $this->view->form = $formProducer;
        }
        else
        {
            if(isset($Producer))                                                 //nếu đối tượng tồn tại
            {
                $UC = new Utility_Converter();                                  //tạo công cụ giúp đổi đối tượng thành mảng
                $formProducer->populate($UC->objectToArray($Producer));           //đổi đối tượng thành mảng rồi gán cho form vừa tạo
                $this->view->form = $formProducer;
            }
            else
            {
                $this->_redirector->gotoUrl('Producer/index');
            }
        }
    }
    
    public function deleteAction(){
        $id = $this->getRequest()->getParam('id', '0');
        $Producer = $this->modelProducer->_find($id);
        if(isset ($Producer)){
            $this->modelProducer->_delete($id);
        }
        $this->_redirector->gotoUrl('Producer/index');
    }
}
?>
